package review.generate;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Map.Entry;

public class Config {
  public static Map<String, Object> readConfig(String config) {
    ResourceBundle bundle = ResourceBundle.getBundle(config);
    Map<String, Object> mapConfig = new HashMap<>();
    Enumeration<String> keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
      String key = keys.nextElement();
      if (key.startsWith("entity.property")) {
        String[] propertys = key.split("[.]");
        PropertyBean p = null;
        Object bean = mapConfig.get(propertys[2]);
        if (bean == null) {
          p = new PropertyBean();
          mapConfig.put(propertys[2], p);
        } else {
          p = (PropertyBean) bean;
        }

        p.set(propertys[3], bundle.getString(key));
      } else {
        mapConfig.put(key, bundle.getString(key));
      }
    }
    return mapConfig;
  }

  public static Map<String, Object> readModelConfig(String config) {
    ResourceBundle bundle = ResourceBundle.getBundle(config);
    Map<String, Object> mapConfig = new HashMap<>();
    mapConfig.put("hasFile", false);
    Enumeration<String> keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
      String key = keys.nextElement();
      if (key.startsWith("model.property")) {
        String[] propertys = key.split("[.]");
        PropertyBean p = null;
        Object bean = mapConfig.get(propertys[2]);
        if (bean == null) {
          p = new PropertyBean();
          mapConfig.put(propertys[2], p);
        } else {
          p = (PropertyBean) bean;
        }
        p.set(propertys[3], bundle.getString(key));
        if (propertys[3].equals("type") && bundle.getString(key).equals("file")) {
          mapConfig.put("hasFile", true);
        }
      } else {
        mapConfig.put(key, bundle.getString(key));
      }
    }
    return mapConfig;
  }

  public static String getBasePath() {
    String url = EntityGenerator.class.getResource("/").getPath().replaceAll("^[/]?(.+)/target/classes[/]?$", "$1");
    return url;
  }

  public static Map<String, Object> getEntityModel(Map<String, Object> map) {
    Map<String, Object> result = new HashMap<>();
    List<PropertyBean> beans = new ArrayList<>();
    for (Entry<String, Object> entity : map.entrySet()) {
      String key = entity.getKey();
      if (key.equals("entity.class")) {
        result.put("className", entity.getValue());
      } else if (key.equals("entity.table")) {
        result.put("tableName", entity.getValue());
      } else if (key.equals("package.entity")) {
        result.put("pageName", entity.getValue());
      } else if (key.equals("package.dao")) {
        result.put("packageDao", entity.getValue());
      } else if (entity.getValue() instanceof PropertyBean) {
        beans.add((PropertyBean) entity.getValue());
      } else {
        result.put(key, entity.getValue());
      }
    }
    result.put("properties", beans);
    return result;
  }

}
